package com.example.lcode.backtrack;

import java.util.ArrayList;
import java.util.List;

public class Question797 {

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> subRes = new ArrayList<>();
        subRes.add(0);
        int target = graph.length - 1;
        allPathsSourceTarget(result, subRes, graph, target, 0);
        return result;
    }

    private void allPathsSourceTarget(List<List<Integer>> result, List<Integer> subRes, int[][] graph, int target, int value) {
        if(target == value) {
            result.add(new ArrayList<>(subRes));
            return;
        }
        for (int i = 0; i < graph[value].length; i++) {
            subRes.add(graph[value][i]);
            allPathsSourceTarget(result, subRes, graph, target, graph[value][i]);
            subRes.remove(subRes.size() - 1);
        }
    }

    public static void main(String[] args) {
        Question797 question797 = new Question797();
        int [][]arr = {{1,2},{3},{3},{}};
        List<List<Integer>> lists = question797.allPathsSourceTarget(arr);
        System.out.println(lists);
    }

}
